<?php

/**
 * Description of TourDao_Class
 *
 * @author aziz
 */
require_once $_SERVER['DOCUMENT_ROOT'] . '/portal/domain/Team_Class.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/portal/domain/Season_Class.php';
require_once 'GenericDAO_Class.php';
include 'SeasonDAO_Class.php';
include 'TeamDAO_Class.php';

class TourDAO extends GenericDAO {

    public $seasonDAO;
    public $teamDAO;

    public function __construct() {
        $this->seasonDAO = new SeasonDAO();
        $this->teamDAO = new TeamDAO();
    }

    public function prepareService($module, $id, $object) {
        $this->type = TOUR_SERVICE_MOD;
        $this->module = $module;
        $this->id = $id;
        $this->object = $object;
        $this->__setUp();
    }

    public function loadAll() {
        $start = microtime(true) * 1000;
        $this->prepareService(SERVICE_ALL, null, null);
        $turlar = $this->getRequest();
        for ($i = 0; $i < count($turlar); $i++) {
            $tour = new Tour();
            $tour->setId($turlar[$i]->id);
            $tour->setSeason($this->seasonDao->loadById($turlar[$i]->season->id));
            $tour->setHome($this->teamDao->loadById($turlar[$i]->team->id));
            $tour->setAway($this->teamDao->loadById($turlar[$i]->away->id));

            

            $persons[] = $person;
        }
        $finish = microtime(true) * 1000;
        $this->logger->info("Processing on PersonDAO::loadAll() takes " . substr(($finish - $start), 0, 4) . " ms");
        return $persons;
    }

    public function loadById($id) {
        $start = microtime(true) * 1000;
        $this->prepareService(SERVICE_SINGLE, $id, null);
        $_person = $this->getRequest();
        $person = new Person();
        $person->setId($_person->id);
        $person->setName($_person->name);
        $person->setPosition($_person->position);
        $person->setBirthday($_person->birthday);
        $person->setContratStartDate($_person->contratStartDate);
        $person->setContratFinishDate($_person->contratFinishDate);
        $team = new Team();
        $team->setId($_person->team->id);
        $team->setName($_person->team->name);
        $person->setTeam($team);
        $person->setType($_person->type);
        $country = new Country();
        $country->setId($_person->nationality->id);
        $country->setName($_person->nationality->name);
        $person->setNationality($country);
        $finish = microtime(true) * 1000;
        $this->logger->info("Processing on PersonDAO::loadById($id) takes " . substr(($finish - $start), 0, 4) . " ms");

        return $person;
    }

    public function delete($id) {
        $start = microtime(true) * 1000;
        $this->prepareService(SERVICE_DELETE, $id, null);
        $this->deleteRequest();
        $finish = microtime(true) * 1000;
        $this->logger->info("Processing on PersonDAO::delete($id) takes " . substr(($finish - $start), 0, 4) . " ms");
    }

    public function upsert($person) {
        $start = microtime(true) * 1000;
        $this->prepareService(SERVICE_SAVE, null, $person);
        $this->postRequest();
        $finish = microtime(true) * 1000;
        $this->logger->info("Processing on PersonDAO::upsert() takes " . substr(($finish - $start), 0, 4) . " ms");
    }

}

?>
